﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.Core.Actions
{
    public abstract class BaseAction
    {
        #region Properties

        public Guid SourceLibraryId { get; set; }
        public int[] SelectedIds { get; set; }
        public SPUser CurrentUser 
        {
            get 
            {
                return SPContext.Current.Web.CurrentUser;
            }
        }

        public SPList SourceLibrary 
        {
            get 
            {
                return SPContext.Current.Web.Lists[SourceLibraryId];
            }
        }

        #endregion

        #region Constructors

        public BaseAction(int[] selectedId, Guid SelectedLibrary) 
        {
            this.SourceLibraryId = SelectedLibrary;
            this.SelectedIds = selectedId;
        }

        #endregion

        #region Abstract Methods

        public abstract void Execute(string targetUser);

        #endregion

        #region Implemented Methods

        protected int getDocumentsPostFix(string targetLibrary, string documentName)
        {
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                var docs = (from document in dc.GetList<SolicitudVB>(targetLibrary)
                            where document.Name == documentName
                            select document);
                return docs.Count();
            }
        }

        protected int getDocumentsPostFix(string documentName, SPList currentList)
        {
            SPQuery query = new SPQuery();
            query.Query =string.Format(@"<Where>
                              <And>
                                 <Eq>
                                    <FieldRef Name='FileLeafRef' />
                                    <Value Type='Text'>{0}</Value>
                                 </Eq>
                                 <Eq>
                                    <FieldRef Name='ContentType' />
                                    <Value Type='Text'>{1}</Value>
                                 </Eq>
                              </And>
                           </Where>",documentName, GlobalConstants.CTTN_VOBO);
            SPListItemCollection result = currentList.GetItems(query);
            return result.Count;
        }


        #endregion


    }
}
